using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using FtpLib;
using System.Net;
using SypConst.Garbage;
using System.Collections;

using SypConst.Path;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace SypEngine.TransferHandler
{
    public class SypFtpTransferHandler : SypTransferHandler
    {
        private FtpConnection myFtpConn = null;

        public SypFtpTransferHandler(SetText outMethod, string FtpHostname, int FtpPort, string FtpLogin, string FtpPassword)
            : base(outMethod)
        {
            try
            {
                myFtpConn = new FtpConnection(FtpHostname, FtpPort, FtpLogin, FtpPassword);
                //smyFtpConn.Open(FtpHostname, FtpPort, FtpLogin, FtpPassword, TransferMode);
                myFtpConn.Open();
                myFtpConn.Login();
            }
            catch (FtpException e)
            {
                OutputMethod("FTP Error, makeDir : " + e.ToString(), Output.ERROR);
                throw new SypTransferException();
            }
        }

        public override int[] CheckSynchronizedPictures(string RootDir)
        {
            List<int> ListPicturesId = new List<int>();

            //if (myFtpConn.DirectoryExists(RootDir))
            //{
                myFtpConn.SetCurrentDirectory(RootDir);
                string pictureFolderName = SypPathHandler.CleanDirectoryName(SypPathHandler.GetPath(SypPathMode.ONLINE, SypPathRef.PICTURE_FOLDER, null));
                if (myFtpConn.DirectoryExists( pictureFolderName)){
                    myFtpConn.SetCurrentDirectory(pictureFolderName);
                    FtpFileInfo[] files = myFtpConn.GetFiles("*.jpg");
                    foreach (var remotefile in files)
                    {
                        string cleanName = remotefile.Name.Replace(SypPathHandler.PICTURE_PREF, string.Empty);
                        cleanName = cleanName.Replace(".jpg", string.Empty);
                        int pictureId = -1;
                        int.TryParse(cleanName, out pictureId);
                        if (pictureId != -1)
                        {
                            ListPicturesId.Add(pictureId);
                        }

                    }
                }
            //}

            return ListPicturesId.ToArray();
        }

        public override void MakeDir(string DirPath)
        {
            try
            {

                if (!myFtpConn.DirectoryExists(DirPath))
                    myFtpConn.CreateDirectory(DirPath);
                //myFtpConn.MakeDir(DirPath);
            }
            catch (FtpException e)
            {
                OutputMethod("FTP Error, makeDir : " + e.ToString(), Output.ERROR);
                throw new SypTransferException();
            }
        }

        public override void RemoveDir(string DirPath)
        {
            try
            {
                //myFtpConn.RemoveDir(DirPath);
                if (myFtpConn.DirectoryExists(DirPath))
                    myFtpConn.RemoveDirectory(DirPath);
            }
            catch (FtpException e)
            {
                OutputMethod("FTP Error, removeDir : " + e.ToString(), Output.ERROR);
                throw new SypTransferException();
            }
        }

        public override void DeleteFile(string FilePath)
        {
            try
            {
                //myFtpConn.DeleteFile(FilePath);
                if (myFtpConn.FileExists(FilePath))
                    myFtpConn.RemoveFile(FilePath);
            }
            catch (FtpException e)
            {
                OutputMethod("FTP Error, deleteFile : " + e.ToString(), Output.ERROR);
                throw new SypTransferException();
            }
        }

        public override void SendFile(string LocalFilePath, string RemoteFilePath)
        {
            try
            {
                myFtpConn.PutFile(LocalFilePath, RemoteFilePath);
                //FileInfo FInfo = new FileInfo(LocalFilePath);

                /*switch (FInfo.Extension)
                {
                    case ".css":
                    case ".html":
                    case ".js":
                        myFtpConn.SendFile(LocalFilePath, RemoteFilePath, FTPFileTransferType.ASCII);
                        break;

                    case ".jpg":
                    case ".gif":
                    case ".png":
                    case ".bmp":
                    case ".jpeg":
                        myFtpConn.SendFile(LocalFilePath, RemoteFilePath, FTPFileTransferType.Binary);
                        break;

                    default:
                        myFtpConn.SendFile(LocalFilePath, RemoteFilePath, FTPFileTransferType.ASCII);
                        break;
                }*/
            }
            catch (FtpException e)
            {
                OutputMethod("FTP Error, sendFile : " + e.ToString(), Output.ERROR);
                throw new SypTransferException();
            }
        }

        public override void Close()
        {
            myFtpConn.Close();
        }

        public override void SetCurrentDirectory(string Path)
        {
            try
            {
                if (myFtpConn.DirectoryExists(Path))
                    myFtpConn.SetCurrentDirectory(Path);
            }
            catch (FtpException e)
            {
                OutputMethod("FTP Error, SetCurrentDirectory : " + e.ToString(), Output.ERROR);
                throw new SypTransferException();
            }
        }
    }
}
